package sdc.com.petscalendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.MotionEvent;

public class LaunchActivity extends Activity {
	

	boolean isTouched = false;
	private Handler handler = new Handler();
	private Thread thread;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_launch);
		thread = new Thread(new Runnable() {

			@Override
			public void run() {
				SystemClock.sleep(3000);
				handler.post(action);
			}
		});
		thread.start();

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			stop();
		}
		return super.onTouchEvent(event);
	}

	private Runnable action = new Runnable() {
		public void run() {
			stop();
		}
	};

	private void stop() {
		if (!isTouched) {
			isTouched = true;
			Intent intent = new Intent();
			intent.setClass(LaunchActivity.this, MainActivity.class);
			startActivity(intent);
			finish();
		}
	}

	@Override
	protected void onStop() {
		thread.interrupt();
		super.onStop();
	}

	@Override
	protected void onRestart() {
		if (thread.isInterrupted()) {
			thread = new Thread(new Runnable() {

				@Override
				public void run() {
					SystemClock.sleep(2000);
					handler.post(action);
				}
			});
			thread.start();
		}
		super.onRestart();
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		thread.interrupt();
		super.onDestroy();
	}
}